/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import at.socialconference.Controller;
import at.socialconference.app.model.AConference;
import at.socialconference.app.twitter.TwitterHelper;
import at.socialconference.app.uihelper.AlertHelper;

public class ConferenceTwitterActivity extends Activity {

	private AConference con;
	private ListView tweets;
	private LinearLayout followLayout;
	private Button followBtn;

	public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);
		setContentView(R.layout.twitterconference);

		Bundle b = getIntent().getExtras();
		con = (AConference)b.getParcelable("conference");

		tweets = (ListView) findViewById(R.id.ct_tweets);
		followLayout = (LinearLayout)findViewById(R.id.ct_followlayout);
		followBtn = (Button)findViewById(R.id.ct_btnFollow);

		if(!Controller.getUser().checkTwitter()){
			AlertDialog alertDialog = new AlertDialog.Builder(this).create();

			alertDialog.setTitle("No connection to Twitter");
			alertDialog.setMessage("Twitter is not activated, please subscribe");
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(ConferenceTwitterActivity.this, TwitterSettingsActivity.class);
					startActivity(intent);
				}
			});
			alertDialog.show();
		}else{

			boolean follows = TwitterHelper.isFollowing(con.getId());
			if(follows){
				initList();
			}else{
				initFollowLayout();
			}
		}
	}

	private void initFollowLayout() {
		tweets.setVisibility(View.GONE);
		followBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int result = TwitterHelper.follow(con.getId());
				if(result>0)
					initList();
				else
					AlertHelper.alert("Failure", "A failure appeared", false, ConferenceTwitterActivity.this);
			}
		});
	}

	private void initList() {
		followLayout.setVisibility(View.GONE);
		
		List<String> statuses = TwitterHelper.getTimelines(con.getId());
		
		tweets.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,statuses));
	}

}
